<template>
  <div class="app-container">
    <!-- 筛选的内容-->
    <div class="filter-container">
      <el-input v-model="QueryObj.name" placeholder="备注" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="QueryObj.type" placeholder="状态" class="filter-item" clearable @change="handleFilter">
        <el-option
          v-for="item in EndTypes"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-delete" @click="handleClear">
        重置
      </el-button>
    </div>

    <!-- 表格内容-->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="名称" width="150" align="left">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" width="80" align="left">
        <template slot-scope="{row}">
          <span>{{ getLabel("EndType",row.type) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到期日期" width="160" align="left">
        <template slot-scope="{row}">
          <span>{{ row.endtime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到期剩余时长" width="160" align="left">
        <template slot-scope="{row}">
          <span>{{ row.laset }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="160" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createtime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" width="160" align="center">
        <template slot-scope="{row}">
          <span>{{ row.updatetime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页内容-->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getList" />

    <!-- dialog内容-->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">

        <el-form-item label="名称" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="temp.type" placeholder="请选择" clearable>
            <el-option
              v-for="item in EndTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
       <el-form-item label="工作日期" prop="endtime">
          <el-date-picker
            v-model="temp.endtime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期"
          />
        </el-form-item>
        <!-- <el-form-item label="工作日期" prop="endtime">
          <el-date-picker
            v-model="temp.endtime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期"
          />
        </el-form-item> -->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, update, del, create } from '@/api/core/dateInfo'
import { listItem } from '@/api/base/dictItemInfo'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      Query: {
        obj: {},
        metaInfo: {}
      },
      listQuery: {
        page: 1,
        size: 10
      },
      QueryObj: {
        remark: ''
      },
      importanceOptions: [1, 2, 3],
      EndTypes: [],
      showReviewer: false,
      temp: {
        status: '',
        description: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        name: [{ required: true, message: '名称必填', trigger: 'change' }],
        endtime: [{ required: true, message: '到期时间必填', trigger: 'change' }],
      },
      downloadLoading: false
    }
  },
  created() {
    this.init()
    this.getList()
  },
  methods: {
    init() {
      listItem({ 'value': 'EndType' }).then(response => {
        this.EndTypes = response.obj
      })
    },
    getList() {
      this.listLoading = true
      this.Query.obj = this.QueryObj
      this.Query.metaInfo = this.listQuery
      console.log(this.Query)
      list(this.Query).then(response => {
        console.log(response)
        this.list = response.obj
        this.total = response.metaInfo.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.05 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    resetTemp() {
      this.temp = {
        description: '',
        status: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          create(this.temp).then(response => {
            if (response.state === 'success') {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          update(this.temp).then(response => {
            if (response.state === 'success') {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleDelete(row, index) {
      del(row.id).then(response => {
        if (response.state === 'success') {
          this.dialogFormVisible = false
          this.getList()
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        }
      })
    },
    handleClear() {
      this.QueryObj.remark = ''
      this.QueryObj.status = ''
      this.getList()
    },
    getLabel(type, value) {
      if (type === 'EndType') {
        // 使用 find() 方法遍历数组，找到匹配的对象
        const found = this.EndTypes.find(item => item.value === value)

        // 如果找到匹配的项，返回 label，否则返回一个提示
        return found ? found.label : ''
      }
    }
  }
}
</script>

